package com.ruibang.glass.material.service.impl.rq;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.material.constant.MaterialErrorCode;
import com.ruibang.glass.material.domain.req.rq.ATypeWaterReq;
import com.ruibang.glass.material.entity.MaterialLineSideWarehouseLocation;
import com.ruibang.glass.material.entity.MaterialWarehouseInfo;
import com.ruibang.glass.material.entity.rq.MaterialStatusManagement;
import com.ruibang.glass.material.mapper.GMaterialLineSideWarehouseLocationMapper;
import com.ruibang.glass.material.mapper.GMaterialWarehouseInfoMapper;
import com.ruibang.glass.material.mapper.rq.MaterialStatusManagementMapper;
import com.ruibang.glass.material.service.rq.MaterialStatusManagementService;
import com.teaming.cloud.framework2.common.exception.ServiceException;
import net.bytebuddy.implementation.bytecode.Throw;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 容器状态管理 服务实现类
 * </p>
 *
 * @author mameng
 * @since 2024-04-02
 */
@Service
public class MaterialStatusManagementServiceImpl extends ServiceImpl<MaterialStatusManagementMapper, MaterialStatusManagement> implements MaterialStatusManagementService {

    @Autowired
    private GMaterialLineSideWarehouseLocationMapper materialLineSideWarehouseLocationMapper;

    @Override
    public IPage<MaterialStatusManagement> getPage(ATypeWaterReq aTypeWaterReq) {
        LambdaQueryWrapper<MaterialStatusManagement> queryWrapper = Wrappers.<MaterialStatusManagement>lambdaQuery();
        if (StringUtils.isNotBlank(aTypeWaterReq.getStatus())) {
            queryWrapper.like(MaterialStatusManagement::getContainerStatusName, aTypeWaterReq.getStatus());
        }
        Page<MaterialStatusManagement> materialWarehouseInfoPage = baseMapper.selectPage(new Page<>(aTypeWaterReq.getPageNo(), aTypeWaterReq.getPageSize()),
                queryWrapper.orderByDesc(MaterialStatusManagement::getCreateTime)
        );
        return materialWarehouseInfoPage;
    }

    @Override
    public void removeMaterialStatusById(String id) {
        Long count = materialLineSideWarehouseLocationMapper.selectCount(Wrappers.<MaterialLineSideWarehouseLocation>
                lambdaQuery().eq(MaterialLineSideWarehouseLocation::getAssociatedContainerStatus, id));
        if (count > 0) {
            throw new ServiceException(MaterialErrorCode.NO_DELETE_RQ_STATUS.getCode(), MaterialErrorCode.NO_DELETE_RQ_STATUS.getMsg());
        }else {
            baseMapper.deleteById(id);
        }
    }
}
